﻿using ConfExpert.Forms.Base;
using DevTeam;
using System;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class StokAlarm : XForm
    {
        public StokAlarm()
        {
            InitializeComponent();
        }

        private void StokAlarm_Load(object sender, EventArgs e)
        {

            TAM.StokAlarmTableAdapter = new ConfExpert.Datasets.MyDataTableAdapters.StokAlarmTableAdapter();
            BindingSource bs = new BindingSource(ds, "StokAlarm");
            InitGridView(gridView1, bs);
            AddBsToList(bs);

            gridView1.Columns["UserId"].Visible = false;
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            gridView1.MakeReadOnly("isActive", "ProductId", "Notes");
            gridView1.AddSummary("BaslangicQty", "GuncelQty");
            gridView1.FormatAsNumber(1, "BaslangicQty", "GuncelQty");
            ds.StokAlarm.TableNewRow += new System.Data.DataTableNewRowEventHandler(StokAlarm_TableNewRow);
            gridView1.ShowingEditor += new System.ComponentModel.CancelEventHandler(gridView1_ShowingEditor);
            btnKaydet.ItemClick += btnKaydet_ItemClick;
            btnGuncelle.ItemClick += btnGuncelle_ItemClick;
            btnSil.ItemClick += btnSil_ItemClick;

            btnGuncelle.PerformClick();
        }

        void btnSil_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            foreach (DataRow row in ds.StokAlarm)
                if (!boolParse(row["isActive"]))
                    row.Delete();

            btnKaydet.PerformClick();
        }

        void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            ds.Clear();
            GetDetails("StokAlarm", "GetStokAlarm", WhoAmI);
            gridView1.BestFitColumns();
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            Save();
            GetDetails("StokAlarm", "UpdStokAlarm", WhoAmI);
            ds.StokAlarm.AcceptChanges();
            gridView1.BestFitColumns();
        }

        void gridView1_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = !(intParse(gridView1.GetFocusedRowCellValue("StokAlarmId")) <= 0 ||
                gridView1.FocusedColumn.FieldName == "isActive" || gridView1.FocusedColumn.FieldName == "Notes");
        }

        void StokAlarm_TableNewRow(object sender, System.Data.DataTableNewRowEventArgs e)
        {
            e.Row["UserId"] = WhoAmI;
            e.Row["isActive"] = true;
        }
    }
}